/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

TileLayer - 地图tile层
修改许可权限 - 江利峰

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function TileLayer()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
package editor.map{
	import editor.common.Common;
	import editor.common.CursorManager;
	import editor.common.UIManager;
	import editor.map.dataOutput.MapdataOutputer;
	import editor.map.elements.Tile;
	import editor.map.minimap.Minimap;
	import editor.util.Boxer;
	import editor.util.NumberTool;
	import editor.util.event.BoxerEvent;
	import editor.vo.TileLayerVO;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	import flash.ui.MouseCursor;
	import flash.utils.Dictionary;
	
	public class TileLayer extends Sprite{
		
		public static var MAP_LOGIC_HEIGHT:int = 50;
		public static var MAP_LOGIC_WIDTH:int = 50;
		public static var MARGIN_R_L_SIZE:int = 840;
		public static var MARGIN_T_B_SIZE:int = 530;
		public static var mapVector:Vector.<uint>;
	
		private var _tilesDic:Dictionary;
		private var _brushSize:int = 1;
		private var _isMouseDown:Boolean;
		private var _box:Boxer;
		
		// 构造函数
		public function TileLayer()
		{
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		/**
		 * 绘制所有tile，根据背景图的大小来定义tile的总数
		 * @param mapWidth
		 * @param mapHeight
		 * 
		 */
		public function drawTiles(mapWidth:Number, mapHeight:Number, datas:Array = null):void
		{
			var tile:Tile;
			if(datas == null)
			{
				mapWidth 	-= mapWidth % Common.pieceWidth;
				mapHeight 	-= mapHeight % Common.pieceHeight;
				MAP_LOGIC_WIDTH = Math.ceil(mapWidth / Tile.TILE_WIDTH);
				MAP_LOGIC_HEIGHT = Math.ceil(mapHeight / Tile.TILE_WIDTH);
			}
			else
			{
				MAP_LOGIC_WIDTH = datas.length;
				MAP_LOGIC_HEIGHT = datas[0].length;
			}
			
			try {removeEventListener(Event.ENTER_FRAME, onEnterFrame);	}
			catch(e:Error){};
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			while(numChildren)
				removeChildAt(0);
			this.graphics.clear();
			
			_tilesDic = new Dictionary();
			mapVector = new Vector.<uint>();
			
			this.graphics.lineStyle(2);
			this.graphics.drawRect(0,0,MAP_LOGIC_WIDTH * Tile.TILE_WIDTH, MAP_LOGIC_HEIGHT * Tile.TILE_WIDTH);
			this.graphics.endFill();
			this.graphics.lineStyle(1);
			
			//画纵向网格横向竖线
			for(var i:int = 0; i < MAP_LOGIC_WIDTH; i ++)
			{
				this.graphics.moveTo((i + 1) * Tile.TILE_WIDTH, 0);
				this.graphics.lineTo((i + 1) * Tile.TILE_WIDTH, MAP_LOGIC_HEIGHT * Tile.TILE_WIDTH);
			}
			
			//画纵向网格纵向竖线
			for(i = 0; i < MAP_LOGIC_HEIGHT; i ++)
			{
				this.graphics.moveTo(0, (i + 1) * Tile.TILE_WIDTH);
				this.graphics.lineTo(MAP_LOGIC_WIDTH * Tile.TILE_WIDTH, (i + 1) * Tile.TILE_WIDTH);
			}
			
			//填充mapVector数据
			
			for(i = 0; i < MAP_LOGIC_WIDTH * MAP_LOGIC_HEIGHT; i++)
			{
				mapVector.push(Minimap.FALSE_COLOR);
			}
			
			if(datas!= null)
			{
				for(i = 0; i < MAP_LOGIC_WIDTH; i ++)
				{
					for(var j:int = 0; j < MAP_LOGIC_HEIGHT; j ++)	
					{
						if(datas[i][j] == true)
							drawOneTile(i,j);
					}
				}
				//
			}
			
			_box = new Boxer(this);
			_box.addEventListener(BoxerEvent.BOX_OVER, onBoxOver);
			addChild(_box);
			UIManager.getInstance().layerControllerWindow.addMinimap();
		}
		
		/**
		 * 输出当前的节点数据 
		 */		
		public function outputTileData():void
		{
			if(_tilesDic != null)
			{
				var tileInfos:Array = new Array();
				var tile:Tile;
				
				for(var i:int = 0; i < MAP_LOGIC_WIDTH; i ++)
				{
					tileInfos[i] = new Array();
					for(var j:int = 0; j < MAP_LOGIC_HEIGHT; j ++)
					{
						if(_tilesDic.hasOwnProperty(i + "," + j))
							tileInfos[i][j] = true;
						else
							tileInfos[i][j] = false;
					}
				}
				
				MapdataOutputer.outputTileBitmapdata(tileInfos);
			}
		}
		
		/**
		 * 根据读取的数据来绘制tiles
		 * @param datas 
		 */		
		public function inputTileData(datas:Array):void
		{
			drawTiles(0, 0, datas);
		}
		
		public function set brushSize(value:int):void
		{
			_brushSize = value;
		}
		
		public function onMouseDown(evt:MouseEvent):void
		{
			if(Common.cursor.state == CursorManager.STATE_NONE)
			_isMouseDown = true;
		}
		
		public function onMouseUp(evt:MouseEvent):void
		{
			_isMouseDown = false;
		}
		
		
		private function onEnterFrame(evt:Event):void
		{
			//实现鼠标拖动操作
			if(_isMouseDown && !_box.isWorking)
			{
				tileWalkableChange(mouseX, mouseY);
			}
		}
		
		//		/**
		//		 * @private
		//		 * 单击某个tile
		//		 * @param evt 
		//		 */		
		//		private function onTileClick(evt:MouseEvent):void
		//		{
		//			var tile:Tile = evt.currentTarget as Tile;
		//			tileWalkableChange(tile);
		//		}
		
		//		private function onTileMouseOver(evt:MouseEvent):void
		//		{
		//			var tile:Tile = evt.currentTarget as Tile;
		//			UIManager.getInstance().tileInfoWindow.updateInfo(tile.lx, tile.ly, tile.walkable);
		//			
		//			//实现鼠标拖动操作
		//			if(_isMouseDown && !_box.isWorking)
		//			{
		//				tileWalkableChange(tile);
		//			}
		//		}
		
		//框选结束
		private function onBoxOver(evt:BoxerEvent):void
		{
			var startLx:int = evt.startPoint.x / Tile.TILE_WIDTH;
			var startLy:int = evt.startPoint.y / Tile.TILE_WIDTH;
			var endLx:int = Math.ceil(evt.endPoint.x / Tile.TILE_WIDTH);
			var endLy:int = Math.ceil(evt.endPoint.y / Tile.TILE_WIDTH);
			
			startLx = NumberTool.limitInt(startLx, 0, MAP_LOGIC_WIDTH - 1);
			startLy = NumberTool.limitInt(startLy, 0, MAP_LOGIC_HEIGHT - 1);
			endLx = NumberTool.limitInt(endLx, 0, MAP_LOGIC_WIDTH);
			endLy = NumberTool.limitInt(endLy, 0, MAP_LOGIC_HEIGHT);
			
			var tile:Tile;
			for(var i:int = startLx; i < endLx; i ++)
			{
				for(var j:int = startLy; j < endLy; j ++)
				{
					//					tile = _tiles[i][j];
					//					tileWalkableChange(tile);
				}
			}
			//
		}
		
		
		private function tileWalkableChange(px:int, py:int):void
		{
			var lp:Point = NumberTool.p2lTransform(px, py);
			var slx:int = lp.x;
			var sly:int = lp.y;
			
			var sx:int = NumberTool.limitInt(lp.x, 0, MAP_LOGIC_WIDTH - 1);
			var	ex:int = NumberTool.limitInt(lp.x + _brushSize, 0, MAP_LOGIC_WIDTH);
			var sy:int = NumberTool.limitInt(lp.y, 0, MAP_LOGIC_HEIGHT - 1);
			var ey:int = NumberTool.limitInt(lp.y + _brushSize, 0, MAP_LOGIC_HEIGHT);
			
			for(var i:int = sx; i < ex; i ++)
			{
				for(var j:int = sy; j < ey; j ++)
				{
					if(!Keyboard.capsLock)
					{
						drawOneTile(i,j);
					}
					
					if(Keyboard.capsLock)
					{
						removeOneTile(i,j)
					}
				}
			}
			//
		}
		
		private function drawOneTile(lx:int, ly:int):void
		{
			if(!_tilesDic.hasOwnProperty(lx + "," + ly))
			{
				var tile:Tile = new Tile();
				tile.draw();
				tile.x = lx * Tile.TILE_WIDTH;
				tile.y = ly * Tile.TILE_WIDTH;
				addChild(tile);
				_tilesDic[lx + "," + ly] = tile;
				mapVector[lx + MAP_LOGIC_WIDTH * ly] = Minimap.TRUE_COLOR;
			}
		}
		
		private function removeOneTile(lx:int, ly:int):void
		{
			if(_tilesDic.hasOwnProperty(lx + "," + ly))
			{
				var tile:Tile = _tilesDic[lx + "," + ly];
				if(contains(tile))
				{
					removeChild(tile);
					mapVector[lx + MAP_LOGIC_WIDTH * ly] = Minimap.FALSE_COLOR;
				}
				delete _tilesDic[lx + "," + ly];
				
			}
		}
		//
	}
}